package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult insert(ApAuthor apAuthor) {
        //1.检查数据
        if (apAuthor==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数异常");
        }

        //2. 保存

        save(apAuthor);

        //3.返回结果

        return ResponseResult.okResult(apAuthor);
    }

    
    @Override
    public ResponseResult findByUserId(Integer userId) {
        //1. 检查参数
        if (userId==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数异常");
        }

        //2. 条件查询

        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ApAuthor::getUserId, userId);

        ApAuthor author = getOne(queryWrapper);

        //3. 返回结果

        return ResponseResult.okResult(author);
    }
}
